package com.dykj.apps.web.sysmanager.action;

import java.util.List;
import java.util.Map;

import com.dykj.apps.web.core.BaseAction;
import com.dykj.apps.web.sysmanager.dto.SysAuthDTO;
import com.dykj.apps.web.sysmanager.service.IAuthManagerBS;
import com.dykj.apps.web.sysmanager.service.IUserManagerBS;
import com.dykj.apps.web.sysmanager.service.IGroupInfoBS;
import com.dykj.platform.framework.exception.ApplicationException;

public class SysAuthAction extends BaseAction{

	private String radio_id;
	private SysAuthDTO auth =new SysAuthDTO();
	private IAuthManagerBS bs;
	private IUserManagerBS userbs;
	private IGroupInfoBS groupbs;
	private List nocheckUserList;
	private List checkUserList;
	private String userList;
	private String groupList;
	private List nocheckGroupList;
	private List checkGroupList;
	
	/**
	 * 添加新权限
	 * @return
	 * @throws ApplicationException
	 */
	public String initAddAuth()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String groupid = info.get("groupid").toString();
		String type = info.get("type").toString();
		String cauthid = info.get("authid").toString();
		Map rmap = bs.addAuthInit(type, groupid, cauthid);
		nocheckUserList = (List) rmap.get("nocheckUser"); //可选用户
		nocheckGroupList = (List) rmap.get("nocheckGroup"); //可选机构
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "authManager");//action名称
		this.request("bn_value", "新增");//按钮名称
		this.request("bn_method", "saveAuth;");//按钮的方法
		return "forwardinitAddauth";
	}
	/**
	 * 保存权限
	 * @return
	 * @throws ApplicationException
	 */
	public String saveAuth()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String username=info.get("username").toString();
		if(auth.getAuthdesc()==null){
			alert("权限描述不能为空");
		}
		else{
			auth.setCreator(username);
			bs.saveAuth(auth,userList.split(";"),groupList.split(";"));
			auth.init();
		}
		alert("保存成功！"); //提示信息
		return queryForPage();
	}
	
	/**
	 * 查看权限信息
	 * @return
	 */
	public String viewAuth()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String groupid = info.get("groupid").toString();
		String type = info.get("type").toString();
		String cauthid = info.get("authid").toString();
		String authid = radio_id;
		Map rmap = bs.editAuthInit(authid,type, groupid, cauthid);
		nocheckUserList = (List) rmap.get("nocheckUser"); //可选用户
		nocheckGroupList = (List) rmap.get("nocheckGroup"); //可选机构
		checkGroupList = (List) rmap.get("checkGroup");//已选用户
		checkUserList = (List) rmap.get("checkUser");//已选机构
		SysAuthDTO sysauth = bs.getOneSysAuth(Long.parseLong(radio_id));
		sysauth.setP_authdesc(auth.getP_authdesc());
		sysauth.setP_authid(auth.getP_authid());
		auth = sysauth;
		this.request("editable", "false");//表单是不可编辑（默认）
		this.request("action", "authManager");//action名称
		this.request("bn_value", "返回");//按钮名称
		this.request("bn_method", "queryForPage");//按钮的方法
		return "forwardinitAddauth";
	}
	/**
	 * 初始化修改权限
	 * @return
	 */
	public String initUpdateAuth() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String groupid = info.get("groupid").toString();
		String type = info.get("type").toString();
		String cauthid = info.get("authid").toString();
		String authid = radio_id;
		Map rmap = bs.editAuthInit(authid , type, groupid, cauthid);
		nocheckUserList = (List) rmap.get("nocheckUser"); //可选用户
		nocheckGroupList = (List) rmap.get("nocheckGroup"); //可选机构
		checkGroupList = (List) rmap.get("checkGroup");
		checkUserList = (List) rmap.get("checkUser");
		SysAuthDTO sysauth = bs.getOneSysAuth(Long.parseLong(radio_id));
		sysauth.setP_authdesc(auth.getP_authdesc());
		sysauth.setP_authid(auth.getP_authid());
		auth = sysauth;
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "authManager");//action名称
		this.request("bn_value", "修改;返回");//按钮名称
		this.request("bn_method", "updateAuth;queryForPage");//按钮的方法
		return "forwardinitAddauth";
	}
	/**
	 * 保存修改权限
	 * @return
	 */
	public String updateAuth()throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String groupid = info.get("groupid").toString();
		String type = info.get("type").toString();
		String cauthid = info.get("authid").toString();
		bs.updateAuth(auth,userList.split(";"),groupList.split(";"),groupid,type);
		String authid = radio_id;
		Map rmap = (Map)bs.editAuthInit(authid,type, groupid, cauthid);
		nocheckUserList = (List) rmap.get("nocheckUser"); //可选用户
		nocheckGroupList = (List) rmap.get("nocheckGroup"); //可选机构
		checkGroupList = (List) rmap.get("checkGroup");
		checkUserList = (List) rmap.get("checkUser");
		alert("保存成功！");
		this.request("editable", "true");//表单可编辑（默认）
		this.request("action", "authManager");//action名称
		this.request("bn_value", "修改;返回");//按钮名称
		this.request("bn_method", "updateAuth;queryForPage");//按钮的方法
		return "forwardinitAddauth";
	}
	/**
	 * 删除权限
	 * @return
	 */
	public String deleteAuth()throws ApplicationException{
		int i = bs.delAuth(Long.parseLong(radio_id));
		if(i<0){
			alert("还有用户拥有此数据权限，不能删除");
		}else{
			alert("删除成功！");
		}
		return queryForPage();
	}
	/**
	 * 初始化分页信息
	 * @return
	 */
	public String initQueryAuth()throws ApplicationException{
		this.completeResultList(new SysAuthDTO());
		this.request("action", "authManager");//action名称
		this.request("bn_value", "添加新权限");//按钮名称
		this.request("bn_method", "initAddAuth");//按钮的方法
		return "forwardAuthManager";
	}
	
	/**
	 * 初始化分页信息
	 * @return
	 */
	public String queryForPage() throws ApplicationException{
		Map info = (Map)this.getSession().getAttribute("userinfo");
		String username=info.get("username").toString();
		auth.setCreator(username);
		this.setPageList(bs.querySysAuth(auth, this.getPage())); //把查询的结果放入LIST
		this.completeResultList(new SysAuthDTO());//条数不够补齐
		this.request("pageMethod", "queryForPage");
		this.request("action", "authManager");//action名称
		this.request("bn_value", "查看;修改;删除;添加新权限");//按钮名称
		this.request("bn_method", "viewAuth;initUpdateAuth;deleteAuth;initAddAuth");//按钮的方法
		this.request("bn_check", "true;true;true;false");//提交时是否验证单选按钮
		return "forwardAuthManager";
	}
	/*
	 * ================================get set方法=====================================
	 */
	public Object getModel() {
		return auth;
	}
	public SysAuthDTO getAuth() {
		return auth;
	}
	public void setAuth(SysAuthDTO auth) {
		this.auth = auth;
	}
	public IAuthManagerBS getBs() {
		return bs;
	}
	public void setBs(IAuthManagerBS bs) {
		this.bs = bs;
	}
	public List getCheckUserList() {
		return checkUserList;
	}
	public void setCheckUserList(List checkUserList) {
		this.checkUserList = checkUserList;
	}
	public List getNocheckUserList() {
		return nocheckUserList;
	}
	public void setNocheckUserList(List nocheckUserList) {
		this.nocheckUserList = nocheckUserList;
	}
	public String getRadio_id() {
		return radio_id;
	}
	public void setRadio_id(String radio_id) {
		this.radio_id = radio_id;
	}
	public IUserManagerBS getUserbs() {
		return userbs;
	}
	public void setUserbs(IUserManagerBS userbs) {
		this.userbs = userbs;
	}
	public String getUserList() {
		return userList;
	}
	public void setUserList(String userList) {
		this.userList = userList;
	}
	public IGroupInfoBS getGroupbs() {
		return groupbs;
	}
	public void setGroupbs(IGroupInfoBS groupbs) {
		this.groupbs = groupbs;
	}
	public List getCheckGroupList() {
		return checkGroupList;
	}
	public void setCheckGroupList(List checkGroupList) {
		this.checkGroupList = checkGroupList;
	}
	public List getNocheckGroupList() {
		return nocheckGroupList;
	}
	public void setNocheckGroupList(List nocheckGroupList) {
		this.nocheckGroupList = nocheckGroupList;
	}
	public String getGroupList() {
		return groupList;
	}
	public void setGroupList(String groupList) {
		this.groupList = groupList;
	}
}
